package conf

import (
	"github.com/micro/go-micro/v2/config"
	"github.com/micro/go-micro/v2/config/source/file"
	"os"
	"path/filepath"
)

var (
	DataBase struct {
		Dsn     string `yaml:"dsn"`
		Log     bool   `yaml:"log"`
		MaxIdle int    `yaml:"maxIdle"`
		MaxOpen int    `yaml:"maxOpen"`
	}

	Redis struct {
		Address   string `yaml:"address"`
		MaxIdle   int    `yaml:"maxIdle"`
		MaxActive int    `yaml:"maxActive"`
	}

	Log struct {
		Level string `yaml:"level"`
	}
)

func Init() {

	var f = filepath.Dir(os.Args[0]) + "/conf.yaml"
	//f = "E:/kop/modules/api/conf.yaml"

	fileSource := file.NewSource(
		file.WithPath(f),
	)

	var conf, err = config.NewConfig()
	if err != nil {
		panic(err)
	}

	// Load file source
	if err := conf.Load(fileSource); err != nil {
		panic(err)

		//DataBase.Dsn = "host=192.168.1.145 user=postgres password=123456 dbname=mygame sslmode=disable"
		//DataBase.MaxIdle = 10
		//DataBase.MaxOpen = 100

	} else {

		_ = conf.Get("database").Scan(&DataBase)
		_ = conf.Get("redis").Scan(&Redis)
		_ = conf.Get("log").Scan(&Log)
		//_ = conf.Get("nats").Scan(&Nats)
	}
}
